﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using MQ.Core.Common;
using MQ.Core.Entity;
using MQ.Core.IService;

namespace MQ.Core.Admin.Areas.System.Controllers
{
    /// <summary>
    /// 字典
    /// </summary>
    [Route("api/system/[controller]")]
    public class DictionaryController : ApiController
    {
        private readonly IDictionaryService service;
        private readonly IDictionaryTypeService typeService;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="service"></param>
        /// <param name="typeService"></param>
        public DictionaryController(IDictionaryService service, IDictionaryTypeService typeService)
        {
            this.service = service;
            this.typeService = typeService;
        }

        #region 字典类型
        /// <summary>
        /// 字典类型列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("/type")]
        public object GetTypeList([FromQuery]string query)
        {
            var data = typeService.GetList(query);
            return Success(data);
        }
        /// <summary>
        /// 字典类型树形
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("type/tree")]
        public ActionResult<ResultMsg> GetTypeTree([FromQuery]string query)
        {
            return ResultMsg.Ok(typeService.GetTree(query));
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("type/save")]
        public ActionResult<ResultMsg> Save([FromForm]DictionaryTypeEntity entity)
        {
            typeService.Save(entity);
            return Success();
        }
        #endregion

        #region 字典
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{query}")]
        public ActionResult<ResultMsg> Get([FromQuery]string query)
        {
            return ResultMsg.Ok(service.GetList(query));
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("page")]
        public object GetPageList([FromQuery]Pagination pagination, [FromQuery]string query)
        {
            var data= service.GetPageList(pagination, query);
            return Success(pagination,data);
        }
        /// <summary>
        /// baocun
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("save")]
        public object Save([FromForm]DictionaryEntity entity)
        {
            service.Save(entity);
            return Success();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("delete/{id}")]
        public object Delete([FromForm]long id)
        {
            service.Delete(id);
            return Success();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("delete/{ids}")]
        public object Delete([FromForm]string ids)
        {
            service.Delete(ids);
            return Success();
        }
        #endregion
    }
}